const express = require('express');

// 创建express对象
const app = express();

//创建路由规则
// request对请求报文的封装
// response 对响应报文的封装
app.get('/serve', (request, response) => {
  // 设置响应头，允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*');
  // 设置响应
  response.send('Hello AJAX');
})

app.post('/serve', (request, response) => {
  // 设置响应头，允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*');
  // 设置响应
  response.send('Hello AJAX post');
})
app.all('/json-serve', (request, response) => {
  response.setHeader('Access-Control-Allow-Origin', '*');

  const data = {
    name: 'name 2'
  };
  let str = JSON.stringify(data);

  response.send(str);
});

// fetch 服务
app.all('/fetch-serve', (request, response) => {
  response.setHeader('Access-Control-Allow-Origin', '*');
  response.setHeader('Access-Control-Allow-Headers', '*');
  const data = {
    name: 'fetch serve'
  };
  let str = JSON.stringify(data);
  response.send(str);
});
//监听端口启动服务
app.listen(8000, () => {
  console.log("服务已经启动， 8000端口监听中");
})